#include "gps_usart.h"

u16 point1 = 0;
u16 USART_RX_STA = 0;
char rxdatabufer;
char USART_RX_BUF[USART_REC_LEN];

USARTBuffer gpsUSARTBuffer;
_SaveData Save_Data;

GPIO_InitTypeDef GPS_GPIO_InitStructure;   // GPIO Struct(GPIO结构体)
USART_InitTypeDef GPS_USART_InitStructure; // USART Struct(串口结构体)
NVIC_InitTypeDef GPS_NVIC_InitStructure;   // NVIC Struct(中断结构体)

extern uint8_t AtRxBuffer[512];
extern uint8_t Rxcouter;

void GPS_USART_Init(u32 bound)
{
    // Enable USART1 clock(使能串口3和时钟)
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

    USART_DeInit(USART3);

    // Configure USART3 pins(配置串口1引脚)
    GPS_GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // PINB10
    GPS_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPS_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPS_GPIO_InitStructure);
    GPS_GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; // PINB11
    GPS_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPS_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPS_GPIO_InitStructure);

    // Configure parameters of USART3(串口3参数)
    GPS_USART_InitStructure.USART_BaudRate = bound;
    GPS_USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    GPS_USART_InitStructure.USART_StopBits = USART_StopBits_1;
    GPS_USART_InitStructure.USART_Parity = USART_Parity_No;
    GPS_USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    GPS_USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(USART3, &GPS_USART_InitStructure);

    // NVIC Configuration(中断配置)
    USART_ClearFlag(USART3, USART_FLAG_TC);
    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);  // Open NVIC of USART1(打开串口3的中断)
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // Set NVIC priority(设置NVIC分组)

    GPS_NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
    GPS_NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    GPS_NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
    GPS_NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&GPS_NVIC_InitStructure);

    gpsUSARTBuffer.USART_TX_Index = 0;
    gpsUSARTBuffer.USART_RX_Index = 0;
    gpsUSARTBuffer.USART_TX_Complete = 0;
    gpsUSARTBuffer.USART_RX_Complete = 0;
    gpsUSARTBuffer.USART_TXFlag = 0;
    gpsUSARTBuffer.USART_RXFlag = 0;
    gpsUSARTBuffer.USART_TX_DataSlice = 0;
    gpsUSARTBuffer.USART_RX_DataSlice = 0;

    USART_Cmd(USART3, ENABLE);

    CLR_Buf(); // 清空缓存
}

void USART3_IRQHandler(void) // 串口1中断服务程序
{
    if (USART_GetITStatus(USART3, USART_IT_RXNE) == SET)
    {
        uint8_t Res = USART_ReceiveData(USART3); //(USART3->DR);   //读取接收到的数据
        if (Res == '$')
        {
            point1 = 0;
            gpsUSARTBuffer.USART_RX_Index = 0;
        }
        if (point1 >= USART_REC_LEN - 1)
        {
            point1 = USART_REC_LEN - 1;
        }
        USART_RX_BUF[point1++] = Res;
        gpsUSARTBuffer.USART_RX_Buffer[gpsUSARTBuffer.USART_RX_Index] = Res;
        gpsUSARTBuffer.USART_RX_Buffer_Slice[gpsUSARTBuffer.USART_RX_Index] = Res;
        gpsUSARTBuffer.USART_RX_Index++;
        gpsUSARTBuffer.USART_RX_DataSlice = Res;
        // 确定是否收到"GPRMC/GNRMC"这一帧数据，或数据过长
        if ((USART_RX_BUF[0] == '$' && USART_RX_BUF[4] == 'M' && USART_RX_BUF[5] == 'C') || gpsUSARTBuffer.USART_RX_Index >= USART_RX_Buffer_Length)
        {
            if (Res == '\n')
            {
                memset(Save_Data.GPS_Buffer, 0, GPS_Buffer_Length); // 清空
                memcpy(Save_Data.GPS_Buffer, USART_RX_BUF, point1); // 保存数据
                memcpy(AtRxBuffer, Save_Data.GPS_Buffer, point1);   // 保存数据
                Rxcouter = point1;                                  // 保存数据长度
                Save_Data.isGetData = true;
                point1 = 0;
                gpsUSARTBuffer.USART_RX_Index = 0;
                gpsUSARTBuffer.USART_RX_Complete = 1;
                memset(USART_RX_BUF, 0, USART_REC_LEN); // 清空
            }
            if (gpsUSARTBuffer.USART_RX_Index >= USART_RX_Buffer_Length)
            {
                gpsUSARTBuffer.USART_RX_Index = 0;
                gpsUSARTBuffer.USART_RX_Complete = 1;
            }
        }
        // Clear USART Buffer(清除串口3的缓冲区)
        USART_ClearITPendingBit(USART3, USART_IT_RXNE);
    }
}

u8 Hand(char *a) // 串口命令识别函数
{
    if (strstr(USART_RX_BUF, a) != NULL)
        return 1;
    else
        return 0;
}

void CLR_Buf(void) // 串口缓存清理
{
    memset(USART_RX_BUF, 0, USART_REC_LEN); // 清空
    point1 = 0;
}

void clrStruct()
{
    Save_Data.isGetData = false;
    Save_Data.isParseData = false;
    Save_Data.isUsefull = false;
    memset(Save_Data.GPS_Buffer, 0, GPS_Buffer_Length); // 清空
    memset(Save_Data.UTCTime, 0, UTCTime_Length);
    memset(Save_Data.latitude, 0, latitude_Length);
    memset(Save_Data.N_S, 0, N_S_Length);
    memset(Save_Data.longitude, 0, longitude_Length);
    memset(Save_Data.E_W, 0, E_W_Length);
}
